<?php

namespace app\admin\model;

use think\Model;


class ShareStrategy extends Model
{
    // 表名
    protected $name = 'share_strategy';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    //关联策略交易表
    public function strategyTransaction()
    {
        return $this->hasMany('StrategyTransaction','strategy_id','id');
    }
    
    
    public function getStatusList()
    {
        //return ['1' => __('Status 1'), '2' => __('Status 2'),'3'=>'亏损预警'];
        return ['1' => __('持仓'), '0' => __('平仓'),'3'=>'亏损预警'];
    }

    public function getIsAutoList()
    {
        return ['1' => __('Is_auto 0'), '1' => __('Is_auto 1')];
    }

    public function getPayTypeList()
    {
        return ['1' => __('Pay_type 1'), '2' => __('Pay_type 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsAutoTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_auto']) ? $data['is_auto'] : '');
        $list = $this->getIsAutoList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBuyTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['buy_time']) ? $data['buy_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setBuyTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    
    
    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
